import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
/* Layout */
import Layout from '@/layout'
Vue.use(Router)

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-ADMIN-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['USER', 'ADMIN', 'SUPER_ADMIN']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    title: '登录',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    name: 'register',
    path: '/register',
    title: '注册',
    component: () => import('@/views/register/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },
  {
    name: 'information',
    title: '个人中心',
    path: '/information',
    redirect: '/information',
    component: Layout,
    hidden: true,
    children: [
      {
        path: 'index',
        name: 'Information',
        component: () => import('@/views/information/index'),
        meta: { title: '个人中心' },
        hidden: true
      }
    ]
  }
]

export const asyncRoutes = [
  {
    name: 'lostfound',
    title: '查看失物',
    path: '/lostfound',
    component: Layout,
    redirect: '/lostfound',
    meta: {
      title: '查看失物',
      icon: 'lock',
      roles: ['USER']
    },
    children: [
      {
        path: 'index',
        name: 'LostFound',
        component: () => import('@/views/LostFound/index'),
        meta: { title: '查看失物', icon: 'lostfound', roles: ['USER'] }
      }
    ]
  },
  {
    name: 'application',
    title: '预约',
    path: '/application',
    component: Layout,
    meta: {
      title: '机房预约',
      icon: 'lock',
      roles: ['USER']
    },
    children: [
      {
        path: 'index',
        name: 'application',
        component: () => import('@/views/application/index'),
        meta: { title: '机房预约', icon: 'user', roles: ['USER'] }
      },
      {
        path: 'UserTable',
        name: 'UserTable',
        component: () => import('@/views/UserTable/index'),
        meta: { title: '预约记录', icon: 'table', roles: ['USER'] }
      }
    ]
  },
  {
    path: '/Maintenance',
    component: Layout,
    meta: {
      title: '日常维护',
      icon: 'lock',
      roles: ['ADMIN', 'SUPER_ADMIN'] // you can set roles in root nav
    },
    children: [
      {
        path: 'MaintenanceLog',
        name: 'MaintenanceLog',
        component: () => import('@/views/MaintenanceLog/index'),
        meta: { title: '维护管理', icon: 'eye', roles: ['ADMIN', 'SUPER_ADMIN'] }
      },
      {
        path: 'submitLog',
        name: 'submitLog',
        component: () => import('@/views/submitLog/index'),
        meta: { title: '上传日志', icon: 'tree', roles: ['ADMIN', 'SUPER_ADMIN'] }
      }
    ]
  },
  {
    path: '/storehouseManage',
    component: Layout,
    meta: {
      title: '物品管理',
      icon: 'lock',
      roles: ['ADMIN', 'SUPER_ADMIN'] // you can set roles in root nav
    },
    children: [
      {
        path: 'storehouseManage',
        name: 'storehouseManage',
        component: () => import('@/views/storehouseManage/index'),
        meta: { title: '仓库管理', icon: 'user', roles: ['ADMIN', 'SUPER_ADMIN'] }
      },
      {
        path: 'lostManage',
        name: 'lostManage',
        component: () => import('@/views/lostManage/index'),
        meta: { title: '失物管理', icon: 'user', roles: ['ADMIN', 'SUPER_ADMIN'] }
      }
    ]
  },

  {
    path: '/accountManage',
    component: Layout,
    meta: {
      title: '账户管理',
      icon: 'lock',
      roles: ['SUPER_ADMIN'] // you can set roles in root nav
    },
    children: [
      {
        path: 'index',
        name: 'accountManage',
        component: () => import('@/views/accountManage/index'),
        meta: { title: '人员管理', icon: 'user', roles: ['SUPER_ADMIN'] }
      }
    ]
  },
  {
    path: '/dutyTable',
    component: Layout,
    meta: {
      title: '值班管理',
      icon: 'lock',
      roles: ['SUPER_ADMIN'] // you can set roles in root nav
    },
    children: [
      {
        path: 'dutyTable',
        name: 'dutyTable',
        component: () => import('@/views/dutyTable/index'),
        meta: { title: '值班表', icon: 'el-icon-setting', roles: ['SUPER_ADMIN'] }
      },
      {
        path: 'checkTable',
        name: 'checkTable',
        component: () => import('@/views/checkTable/index'),
        meta: { title: '机房检查表', icon: 'el-icon-setting', roles: ['SUPER_ADMIN'] }
      },
      {
        path: 'openDoorTable',
        name: 'openDoorTable',
        component: () => import('@/views/openDoorTable/index'),
        meta: { title: '机房开门表', icon: 'el-icon-setting', roles: ['SUPER_ADMIN'] }
      }
    ]
  },
  {
    path: '/example',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: { title: '预约管理', icon: 'el-icon-s-help', roles: ['SUPER_ADMIN'] },
    children: [
      {
        path: 'table',
        name: 'Table',
        component: () => import('@/views/table/index'),
        meta: { title: '预约记录', icon: 'table', roles: ['SUPER_ADMIN'] }
      }
      // 失物申领记录暂时没有实现
      // {
      //   path: 'shiwutable',
      //   name: 'Shiwutable',
      //   component: () => import('@/views/shiwutable/index'),
      //   meta: { title: '失物申领记录', icon: 'tree', roles: ['ADMIN', 'SUPER_ADMIN'] }
      // }
    ]
  },
  {
    path: '/dutyTableView',
    component: Layout,
    meta: {
      title: '值班情况',
      icon: 'lock',
      roles: ['ADMIN', 'SUPER_ADMIN'] // you can set roles in root nav
    },
    children: [
      {
        path: 'index',
        name: 'dutyTableView',
        component: () => import('@/views/dutyTableView/index'),
        meta: { title: '值班情况', icon: 'user', roles: ['ADMIN', 'SUPER_ADMIN'] }
      },
      {
        path: 'checkTableView',
        name: 'checkTableView',
        component: () => import('@/views/checkTableView/index'),
        meta: { title: '检查表', icon: 'el-icon-setting', roles: ['ADMIN', 'SUPER_ADMIN'] }
      },
      {
        path: 'openDoorTableView',
        name: 'openDoorTableView',
        component: () => import('@/views/openDoorTableView/index'),
        meta: { title: '开门表', icon: 'el-icon-setting', roles: ['ADMIN', 'SUPER_ADMIN'] }
      }
    ]
  },
  {
    path: '/message',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'message',
        component: () => import('@/views/messageboard/index'),
        meta: { title: '留言板', icon: 'eye' }
      }
    ]
  },
  {
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'http://www.cs.gxnu.edu.cn/main.htm',
        meta: { title: '学院官网介绍', icon: 'link' }
      }
    ]
  },

  {
    name: '404',
    path: '/404',
    title: '404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

/*
<el-menu :active-text-color="variables.menuActiveText" :default-active="activeMenu" mode="horizontal" @select="handleSelect">
      <!-- 右侧对象放在这里 -->
      <div v-for="item in routes" :key="item.path" class="nav-item">
        <app-link :to="resolvePath(item)">
          <el-menu-item v-if="!item.hidden&&!item.children" :index="item.path"><span slot="title">{{ item.meta ? item.meta.title : item.children[0].meta.title }}</span></el-menu-item>
        </app-link>
      </div>
    </el-menu>

<el-menu :default-active="activeMenu" mode="horizontal" @select="handleSelect">
      <el-submenu v-for="(item, index) in routes" :key="index" index="1">
        <template slot="title">
          <i :class="item.icon" />{{ item.name }}
        </template>
        <router-link v-for="child in item.children" :key="child.path" :to="resolvePath(child)">
          <el-menu-item v-if="!item.hidden&&!item.children">{{ child.meta.title }}</el-menu-item>
        </router-link>
      </el-submenu>
    </el-menu>

    <el-menu :active-text-color="variables.menuActiveText" :default-active="activeMenu" mode="horizontal" @select="handleSelect">
      <!-- 右侧对象放在这里 -->
      <div v-for="(item,index) in routes" :key="item.path" class="nav-item">
        <app-link :to="resolvePath(item)">
          <el-submenu v-if="!item.hidden" v-show="item.title" :index="item.path">
            <template slot="title">
              <i :class="item.icon" />{{ item.title }}
            </template>
            <el-menu-item v-for="child in item.children" :key="child.path" :index="child.path">
              {{ child.meta.title }}
            </el-menu-item>
          </el-submenu>
        </app-link>

        </app-link>
        </el-submenu>

      </div>

    </el-menu>

    关键代码：
    <el-menu :active-text-color="variables.menuActiveText" :default-active="activeMenu" mode="horizontal" @select="handleSelect">
      <!-- 右侧对象放在这里 -->
      <el-submenu v-for="(item,index) in routes" :key="index" :v-if="!item.hidden" :index="item.path" class="nav-item">
        <template v-if="!item.hidden" slot="title">
          <i :class="item.icon" />{{ item.title }}
        </template>
        <app-link v-for="child in item.children" :key="child.path" :to="resolvePath(child)">
          <el-menu-item>
            {{ child.meta.title }}
          </el-menu-item>
        </app-link>
      </el-submenu>
    </el-menu>
    */
